package com.zjq.springsocial.renren.connect;

import org.springframework.social.ApiException;
import org.springframework.social.connect.ApiAdapter;
import org.springframework.social.connect.ConnectionValues;
import org.springframework.social.connect.UserProfile;
import org.springframework.social.connect.UserProfileBuilder;

import com.zjq.springsocial.renren.api.Image;
import com.zjq.springsocial.renren.api.ImageSize;
import com.zjq.springsocial.renren.api.Renren;
import com.zjq.springsocial.renren.api.User;

/**
 * 
 * <P>
 * 人人网开放平台ai
 * </P>
 * 
 * @author zhongjingquan
 * 
 * @version 0.0.1-SNAPSHOT 2013-7-18下午2:22:56
 */
public class RenrenApiAdapter implements ApiAdapter<Renren> {

	@Override
	public boolean test(Renren renren) {
		try {
			renren.getUserOperations().getUser();
			return true;
		} catch (ApiException e) {
			return false;
		}
	}

	@Override
	//http://www.renren.com/544851288/profile
	public void setConnectionValues(Renren renren, ConnectionValues values) {
		User user = renren.getUserOperations().getUser();
		values.setProviderUserId(Long.toString(user.getId()));
		values.setDisplayName("@" + user.getName());
		values.setProfileUrl("http://www.renren.com/" + user.getId() + "/profile");
		Image[] images = user.getAvatar();
		String imageUrl = "";
		if(images != null && images.length > 0){
			for(Image image : images){
				if(image.getSize() == ImageSize.HEAD){
					imageUrl = image.getUrl();
					break;
				}
			}
		}
		values.setImageUrl(imageUrl);
	}

	@Override
	public UserProfile fetchUserProfile(Renren renren) {
		User profile = renren.getUserOperations().getUser();
		return new UserProfileBuilder().setName(profile.getName()).setUsername(profile.getName()).build();
	}

	@Override
	public void updateStatus(Renren api, String message) {
	}

}
